package com.jnu.keepaccounts;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jnu.keepaccounts.data.Data;
import com.jnu.keepaccounts.data.DataBank;
import com.jnu.keepaccounts.data.DataTiaoMu;
import com.jnu.keepaccounts.data.DataZhuanZhang;

import java.util.ArrayList;
import java.util.Calendar;

public class AccountSee extends AppCompatActivity {
    private String account_name;
    private ArrayList<Data> dataArrayList= new ArrayList<>();
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountsee);


        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        Intent intent = getIntent() ;
        account_name = intent.getStringExtra("name");
        double money = intent.getDoubleExtra("money",0);
        TextView textView = findViewById(R.id.account_yue);
        textView.setText(money+"");

        ImageButton imageButton = findViewById(R.id.back);
        imageButton.setOnClickListener(v -> finish());

        TextView textView1 = findViewById(R.id.title);
        textView1.setText(account_name);
        DataBank dataBank = new DataBank(getApplicationContext());
        ArrayList<DataZhuanZhang> dataZhuanZhangs = dataBank.loadZData();
        for(Data data :dataBank.loadData()){
            if(data.getFlag()==1){
                DataTiaoMu dataTiaoMu = (DataTiaoMu) data;
                if(dataTiaoMu.getLaiYuan().equals(account_name)){
                    dataArrayList.add(data);
                }
            }
        }
        for(DataZhuanZhang dataZhuanZhang:dataZhuanZhangs){
            if(dataZhuanZhang.getIn().equals(account_name)||dataZhuanZhang.getOut().equals(account_name)){
                dataArrayList.add(dataZhuanZhang);
            }
        }
        dataArrayList.sort((e1,e2)-> {
            Calendar calendar = Calendar.getInstance();
            calendar.set(e1.getYear(),e1.getMonth(),e1.getDay());
            Long tem1 = calendar.getTimeInMillis();
            calendar.set(e2.getYear(),e2.getMonth(),e2.getDay());
            Long tem2 = calendar.getTimeInMillis();
            if(tem1>tem2) return -1;
            else if(tem1<tem2)return 1;
            else {
                return (int) (e2.getDate()-e1.getDate());
            }
        });

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView_account_jilu);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        NewAdapter newAdapter = new NewAdapter(dataArrayList);
        recyclerView.setAdapter(newAdapter);

    }
    public class NewAdapter extends RecyclerView.Adapter<NewAdapter.ViewHolder>{
        private ArrayList<Data> datas;
        public NewAdapter(ArrayList<Data> datas){
            this.datas = datas;
        }
        @NonNull

        @Override
        public NewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.accounttiao,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull NewAdapter.ViewHolder holder, int position) {
            Data data = datas.get(position);
            if(data.getFlag()==1){
                if(((DataTiaoMu)data).getLaiYuan().equals(account_name)){
                    DataTiaoMu dataTiaoMu = (DataTiaoMu) data;
                    holder.tou.setText(dataTiaoMu.getXiangMu());
                    holder.date.setText(dataTiaoMu.getYear()+"-"+dataTiaoMu.getMonth()+"-"+dataTiaoMu.getDay());
                    holder.money.setText(dataTiaoMu.getShuE()+"");
                }
            }
            else if(data.getFlag() == 3){
                if(((DataZhuanZhang)data).getIn().equals(account_name)){
                    DataZhuanZhang dataZhuanZhang= (DataZhuanZhang) data;
                    holder.tou.setText("收到"+dataZhuanZhang.getOut()+"转账");
                    holder.date.setText(data.getYear()+"-"+data.getMonth()+"-"+data.getDay());
                    holder.money.setText(dataZhuanZhang.getMoney()+"");
                }
                else if(((DataZhuanZhang)data).getOut().equals(account_name)){
                    DataZhuanZhang dataZhuanZhang= (DataZhuanZhang) data;
                    holder.tou.setText("转账到"+dataZhuanZhang.getIn());
                    holder.date.setText(data.getYear()+"-"+data.getMonth()+"-"+data.getDay());
                    holder.money.setText(-dataZhuanZhang.getMoney()+"");
                }
                else;
            }
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder{
            TextView tou,date,money;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tou = (TextView) itemView.findViewById(R.id.mingmu);
                date = (TextView) itemView.findViewById(R.id.date);
                money = (TextView)  itemView.findViewById(R.id.money);
            }
        }
    }
}
